// pages/index/weather/weather.js
const util = require("../../../../utils/util");
const app = getApp();
// const bmap = require('../../../../utils/bmap-wx');
// var BMap = new bmap.BMapWX({
//   ak: 'ntaIuYwqxYZYmwSEUpdOmmwPCy28qnFU'
// });
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
const filterIndex = ["穿衣", "雨伞", "感冒", "晨练", "紫外线强度", "晾晒", "洗车", "钓鱼", "旅游", "运动"];
let toggleCollection = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        forecast_24h: {},
        index: {},
        observe: {},
        region: [],
        scroll: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // util.colleation(this, "天气查询");
        toggleCollection = true;
        let province = options.province ? decodeURIComponent(options.province) : "广东省";
        let city = options.city ? decodeURIComponent(options.city) : "深圳市";
        let county = options.district ? decodeURIComponent(options.district) : "罗湖区";
        if (!options.province && !options.city && !options.district) {
            qqmapsdk.reverseGeocoder({
                success: (res) => {
                    let province = res.result.address_component.province;
                    let city = res.result.address_component.city;
                    let county = res.result.address_component.district;
                    this.setData({
                        region: [province, city, county],
                    });
                    var localInfo = {
                        province,
                        city,
                        county
                    };
                    this.getWeather(localInfo);
                }
            })
        } else {
            this.setData({
                region: [province, city, county],
            });
            var localInfo = {
                province,
                city,
                county
            };
            this.getWeather(localInfo);
        }

        // let province = options.province ? decodeURIComponent(options.province) : app.globalData.province;
        // let city = options.city ? decodeURIComponent(options.city) : app.globalData.city;
        // let county = options.county ? decodeURIComponent(options.county) : (app.globalData.district === '全部' ? '' : app.globalData.district);
        // this.setData({
        //   region: [province, city, county],
        // });
        // var localInfo = { province, city, county };
        // this.getWeather(localInfo);
    },
    getWXLocation() {

    },
    getWeather(localInfo, region) {
        wx.request({
            url: 'https://wis.qq.com/weather/common',
            data: {
                source: "xw",
                weather_type: "observe|forecast_24h|index",
                province: localInfo.province,
                city: localInfo.city,
                county: localInfo.county,
            },
            success: res => {
                if (res.data.data.observe.degree) {
                    let weatherInfo = JSON.parse(JSON.stringify(res.data.data));
                    for (let weather in weatherInfo.forecast_24h) {
                        switch (weather) {
                            case '0':
                                {
                                    weatherInfo.forecast_24h[weather].time = '昨天'
                                }
                                break;
                            case '1':
                                {
                                    weatherInfo.forecast_24h[weather].time = '今天'
                                }
                                break;
                            case '2':
                                {
                                    weatherInfo.forecast_24h[weather].time = '明天'
                                }
                                break;
                            case '3':
                                {
                                    weatherInfo.forecast_24h[weather].time = '后天'
                                }
                                break;
                            default:
                                {
                                    weatherInfo.forecast_24h[weather].time = `周${this.getWeek(new Date(weatherInfo.forecast_24h[weather].time).getDay())}`;
                                }
                        };
                        weatherInfo.forecast_24h[weather].icon = `https://mat1.gtimg.com/pingjs/ext2020/weather/pc/icon/weather/day/${weatherInfo.forecast_24h[weather].day_weather_code}.png`
                    }
                    let index = [];
                    for (let itemIndex in weatherInfo.index)
                        if (filterIndex.findIndex(v => weatherInfo.index[itemIndex].name === v) >= 0) {
                            if (weatherInfo.index[itemIndex].name !== "紫外线强度")
                                weatherInfo.index[itemIndex].name = `${weatherInfo.index[itemIndex].name}指数`;
                            index.push(weatherInfo.index[itemIndex])
                        }
                    this.setData({
                        forecast_24h: weatherInfo.forecast_24h,
                        index,
                        observe: weatherInfo.observe,
                        region
                    });
                } else {
                    wx.showToast({
                        title: '暂无天气信息',
                        icon: 'none',
                    });
                }
            },
        });
    },
    getWeek(day) {
        const week = ['日', '一', '二', '三', '四', '五', '六'];
        return week[day];
    },

    pickCity(e) {
        var localInfo = {
            province: e.detail.value[0],
            city: e.detail.value[1],
            county: e.detail.value[2],
        };
        this.getWeather(localInfo, e.detail.value);
    },
    onShareAppMessage: function() {
        return {
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: "本地宝天气查询",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    }
})